package com.baijiayun.liveuibase.toolbox.bonuspoints

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ListView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.baijiahulian.common.cropperv2.uikit.CircleColorView
import com.baijiayun.livecore.context.LPConstants
import com.baijiayun.livecore.models.LPBonusPointsRankList
import com.baijiayun.liveuibase.R
import com.baijiayun.liveuibase.base.BaseAdapter
import com.baijiayun.liveuibase.base.ViewHolder
import com.baijiayun.liveuibase.utils.DisplayUtils

/**
 * Created by pengwenjian on 2021/7/23
 * Describe:
 */
class BonusPointsFragment(val isAdmin: Boolean? = false) : Fragment() {
    private var listView: ListView? = null
    private var adapter: BaseAdapter<LPBonusPointsRankList.LPBonusPointsRank>? = null
    private var emptyLayout: View? = null
    private var singleLayout: View? = null
    private var bonusPointsRankTv: TextView? = null
    private var bonusPointsTv: TextView? = null
    var bonusPointsRankList: LPBonusPointsRankList? = null
        set(value){
            field = value
            if (isAdmin == true) {
                adapter?.setNewData(value?.rankList)
            } else {
                if (value?.rankList?.size ?: 0 != 0) {
                    val bonusPointsRank = value?.rankList?.get(0)
                    bonusPointsRankTv?.text =
                        getString(R.string.base_rank_infix, bonusPointsRank?.rank)
                    bonusPointsTv?.text = (bonusPointsRank?.points ?: 0).toString()
                } else {
                    bonusPointsRankTv?.text = getString(R.string.base_rank_unknown)
                }
            }
        }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.uibase_layout_bonus_points_list, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        listView = view.findViewById(R.id.list_view)
        emptyLayout = view.findViewById(R.id.layout_empty)
        singleLayout = view.findViewById(R.id.layout_single)
        bonusPointsRankTv = view.findViewById(R.id.bonus_points_rank_tv)
        bonusPointsTv = view.findViewById(R.id.bonus_points_tv)
        if (isAdmin == true) {
            listView?.visibility = View.VISIBLE
            singleLayout?.visibility = View.GONE
            initListView()
        } else {
            listView?.visibility = View.GONE
            singleLayout?.visibility = View.VISIBLE
            if (bonusPointsRankList?.rankList?.size ?: 0 != 0) {
                val bonusPointsRank = bonusPointsRankList?.rankList?.get(0)
                bonusPointsRankTv?.text = getString(R.string.base_rank_infix, bonusPointsRank?.rank)
                bonusPointsTv?.text = (bonusPointsRank?.points ?: 0).toString()
            } else {
                bonusPointsRankTv?.text = getString(R.string.base_rank_unknown)
            }
        }
    }

    private fun initListView() {
        listView?.run {
            divider = ColorDrawable(ContextCompat.getColor(context, R.color.base_divider_line_other))
           dividerHeight = DisplayUtils.dip2px(context, 0.5f)
            emptyView = emptyLayout
            this@BonusPointsFragment.adapter =
                object : BaseAdapter<LPBonusPointsRankList.LPBonusPointsRank>(
                    context,
                    R.layout.uibase_item_bonus_points_rank,
                    bonusPointsRankList?.rankList
                ) {
                    override fun convert(
                        helper: ViewHolder,
                        item: LPBonusPointsRankList.LPBonusPointsRank,
                        position: Int
                    ) {
                        var points = item.points.toString()
                        if (bonusPointsRankList?.type == LPConstants.BonusPointType.TYPE_GROUP) {
                            if (item.points == 0L) {
                                points = "-"
                            }
                        } else {
                            helper.convertView.visibility =
                                if (item.points > 0) View.VISIBLE else View.GONE
                        }

                        helper.setText(R.id.user_name_tv, item.name)
                        helper.setText(R.id.points_tv, points)
                        val rankTv = helper.getView<TextView>(R.id.rank_tv)
                        when (item.rank) {
                            1 -> {
                                rankTv.setBackgroundResource(R.drawable.uibase_ic_study_rank_1)
                                rankTv.text = ""
                            }
                            2 -> {
                                rankTv.setBackgroundResource(R.drawable.uibase_ic_study_rank_2)
                                rankTv.text = ""
                            }
                            3 -> {
                                rankTv.setBackgroundResource(R.drawable.uibase_ic_study_rank_3)
                                rankTv.text = ""
                            }
                            else -> {
                                rankTv.background = null
                                rankTv.text = item.rank.toString()
                            }
                        }
                        //小组排名显示组颜色
                        val circleColorView = helper.getView<CircleColorView>(R.id.circle_color_view)
                        if (isAdmin == true && bonusPointsRankList?.type == LPConstants.BonusPointType.TYPE_GROUP) {
                            //大班课没有颜色,要做需要取本地颜色
                            if (TextUtils.isEmpty(item.color)) {
                                circleColorView.visibility = View.GONE
                            } else {
                                circleColorView.visibility = View.VISIBLE
                                circleColorView.setColor(Color.parseColor(item.color))
                            }
                        } else {
                            circleColorView.visibility = View.GONE
                        }
                    }

                }
            adapter = this@BonusPointsFragment.adapter
        }
    }
}